/**
 * @file
 * @auth jinguangguo
 * @date 2016/12/29
 */

import './style.scss';

import Vue from 'vue';
import BigNumber from 'bignumber.js';
import AnimateNumber from '../../../grsc-base/component/AnimateNum/index';

import filter from '../../../grsc-base/util/filter';

import tpl from './tpl.html';
import commonService from '../../common/commonService';
import symbolConfig from "../../common/symbolConfig";
import lib from "../../common/lib";

export default Vue.extend({
    name: 'CompMenu',
    template: tpl,
    components: {},
    data() {
        return {
            symbolConfig: symbolConfig,
            symbolList: [],
            currentCoins: [],
            filter,
            // forkCoins:null,
            keyCoin: '',
            isActive: symbolConfig.MARKET_TABS.STANDRARD.LABEL
        };

    },
    created() {

        commonService.fetchSymbols((symbolMap, symbolList) => {
            console.log(symbolList)
            this.symbolList = _.cloneDeep(symbolList);
            this._loopTradeDetail();
        });


        // this._connect();


        // $(document).off('$$pan.CoinMenu').on('$$pan.CoinMenu', () => {
        //     this._loopTradeDetailOnce();
        // });


        // this.fetchTradeOpenStatus();
        // this.combineForkCoins();

    },
    mounted() {

    },

    watch: {},
    methods: {

        // _loopTradeDetailOnce() {
        //     commonService.getTradeDetail().done(rep => {
        //         this._doResult(rep);
        //     });
        // },

        /**
         * 循环价格
         * @private
         */
        _loopTradeDetail() {
            commonService.onLoopGetTradeDetail(rep => {
                this._doResult(rep);
            });
        },

        _doResult(rep) {
            let that = this;
            that.isLoading = false;
            if (rep) {
                let arr = rep;
                $.map(this.symbolList, (item, key) => {
                    if (!arr.hasOwnProperty(item.symbol)) {
                        return;
                    }
                    let pr_ne = arr[item.symbol]['newPrice']; // 最新價
                    let pr_op = arr[item.symbol]['24Price']; // 開盤價

                    //最新价
                    item.price = filter.floorFixPrice(pr_ne, item[symbolConfig.PRICEPRECISION]);

                    // 修改涨幅向下取整的问题
                    let distance = pr_ne - pr_op;
                    let symbol = 1;
                    if (distance < 0) {
                        symbol = -1
                    }

                    if (arr[item.symbol]['24Price'] == 0) {
                        item.changeRatio = filter.floorFix(0, 2);
                    } else {
                        item.changeRatio = filter.floorFix(symbol * (filter.abs(distance) / pr_op * 100), 2);
                    }
                });

                this._doCurrentCoins();
            }
        },

        changeType(type) {
            this.isActive = type;
            this.keyCoin = '';
            this._doCurrentCoins();
            localStorage.setItem('setType',type)
        },

        goPage(symbol) {
            window.location.href = './market.html?s=' + lib.revertCoin(symbol);
            localStorage.setItem('market',symbol);
        },

        _doCurrentCoins() {
            let results = [];
            let regions = [];
            //   if (this.isActive !== symbolConfig.MARKET_TABS.USD.LABEL) {
            $.map(this.symbolList, (item, key) => {
                if (item.region === this.isActive) {
                    console.log(item.symbol)
                    if (item.status === symbolConfig.STATUS_LISTED || item.status === symbolConfig.STATUS_DELISTED) {
                        results.push(item);
                    }
                }
                if (item.region === 'INNOVATION') {
                    regions.push(item.symbol);
                }
            });
            localStorage.setItem("regions", JSON.stringify(regions));


// } else {
//     $.map(this.symbolList, (item, key) => {
//
//         if (item.assetCode1 === 'USC' || item.assetCode1 === 'USDT') {
//             if (item.status === symbolConfig.STATUS_LISTED || item.status === symbolConfig.STATUS_DELISTED) {
//                 results.push(item);
//             }
//         }
//
//         if (item.assetCode1 === 'UDC') {
//             results.push(item);
//         }
//     });
// }
            // log(results);

            this.currentCoins = results;

            function log(list) {
                $.map(list, (item, index) => {
                   
                });
            }

        },
    }

});